﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MediaWiki.Office.Model.Utilities;

namespace MediaWiki.Office.Model
{
  public class Table : DocumentContent
  {
    public static readonly XName Name = Document.WordprocessingML + "tbl";

    private List<int> _grid = new List<int>();
    private TableProperties _properties;
    private List<TableRow> _tableRows = new List<TableRow>();

    public List<TableRow> Rows
    {
      get { return _tableRows; }
      set { _tableRows = value; }
    }

    public TableProperties Properties
    {
      get { return _properties; }
      set { _properties = value; }
    }

    public List<int> Grid
    {
      get { return _grid; }
      set { _grid = value; }
    }

    public Table(Content parent, XElement element) : base(parent)
    {
      if (element == null)
        throw new ArgumentNullException("element");

      XNamespace ns = element.Name.Namespace;

      _properties = new TableProperties(this, element.Element(ns + "tblPr"));

      foreach (XElement gridColumnElement in element.Elements(ns + "tblGrid").Elements(ns + "gridCol"))
      {
        _grid.Add((int)gridColumnElement.Attribute(ns + "w"));
      }

      foreach (XElement tableRowElement in element.Elements(ns + "tr"))
      {
        _tableRows.Add(new TableRow(this, tableRowElement));
      }
    }
  }
}